package com.ecas.code.generator.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

public class RuntimeUtil {
    private static final Logger log = LoggerFactory.getLogger(RuntimeUtil.class);

    public static void executeCmdCommonder(String args,String path) {
        String userDir = "D:\\workspace\\ecas\\ecas-code-generator\\proto\\proto3.2";
        File file = new File(userDir);
        String[] envp =new  String[]{"file.encoding=utf-8"};
        try {
            Process process = Runtime.getRuntime().exec(args, envp, file);
            printConsole(process);
        } catch (Exception e) {
            log.error("Runtime.getRuntime().exec, Excepion:", e);
        }
    }

    private static void printConsole(Process process) {
        try {
            InputStream stderr = process.getErrorStream();
            InputStreamReader isr = new InputStreamReader(stderr);
            BufferedReader br = new BufferedReader(isr);
            String line = null;
            while ((line = br.readLine()) != null) {
                log.error(line);
            }
            int exitVal = process.waitFor();
            log.info("Process exitValue: " + exitVal);
        } catch (Exception e) {
            log.error("Exception:",e);
        }
    }


}
